package top.yuhaitao.core.controller;

import com.CarH5Hession.service.getCarH5InfoService;
import com.caucho.hessian.client.HessianProxyFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.yuhaitao.core.controller.RequestModel.RequestUserCarModel;

import java.net.MalformedURLException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * car
 *
 * @author ht.yu
 */
@RestController
@RequestMapping("/carSell")
public class CarSellController {

    private Logger log = LoggerFactory.getLogger(CarSellController.class);


    @Value("${hessian.url}")
    private String url;


    @RequestMapping("/submitSHCarsell")
    public Map<String, Object> submitSHCarsell(@RequestBody RequestUserCarModel requestUserCarModel) {
//        TUserIntentionCar tUserIntentionCar = new TUserIntentionCar();
//        tUserIntentionCar.setRealName(requestUserCarModel.getRealName());
//        tUserIntentionCar.setMobile(requestUserCarModel.getMobile());
//        tUserIntentionCar.setCompany(requestUserCarModel.getCompany());
//        tUserIntentionCar.setCarType(requestUserCarModel.getCarType());
//        tUserIntentionCar.setFinanProduct(requestUserCarModel.getFinanProd());
//        if (!StringUtils.isEmpty(requestUserCarModel.getAddress())) {
//            tUserIntentionCar.setAddress(requestUserCarModel.getAddress());
//        }
//        tUserIntentionCar.setMail(requestUserCarModel.getMail());
//        Date date = new Date();
//        tUserIntentionCar.setCreateTime(date);
//        tUserIntentionCar.setUpdateTime(date);
//        tUserIntentionCarMapper.insertSelective(tUserIntentionCar);


        Map<String, Object> map = new HashMap<>();
        Date date = new Date();
        if (StringUtils.isEmpty(url)) {
            map.put("retCode", "0001");
            map.put("retMsg", "没有配置hessian");
            map.put("success", false);
            map.put("data", date);
            return map;
        }

        if (StringUtils.isEmpty(requestUserCarModel.getRealName()) || StringUtils.isEmpty(requestUserCarModel.getMobile()) ||
                StringUtils.isEmpty(requestUserCarModel.getCompany()) || StringUtils.isEmpty(requestUserCarModel.getCarType()) ||
                StringUtils.isEmpty(requestUserCarModel.getFinanProd()) || StringUtils.isEmpty(requestUserCarModel.getMail()) ||
                StringUtils.isEmpty(requestUserCarModel.getAddress())
        ) {
            map.put("retCode", "0002");
            map.put("retMsg", "参数错误");
            map.put("success", false);
            map.put("data", date);
            return map;
        }


        Map<String, String> mapInfo = new HashMap<String, String>();
        mapInfo.put("CUSTOMERNAME", requestUserCarModel.getRealName()); // 客户姓名
        mapInfo.put("TELEPHONE", requestUserCarModel.getMobile());// 手机号码
        mapInfo.put("COMPANY", requestUserCarModel.getCompany());// 工作单位
        mapInfo.put("PURPOSECAR", requestUserCarModel.getCarType());// 意向车型
        mapInfo.put("PURPOSEPRODUCT", requestUserCarModel.getFinanProd());// 0061:0首付款  0062:长期限制 0063:低月供
        mapInfo.put("EMAIL", requestUserCarModel.getMail());// 邮箱
        mapInfo.put("HOPELICENSESPACE", requestUserCarModel.getAddress());// 意向上牌地


        HessianProxyFactory factory = new HessianProxyFactory();
        factory.setConnectTimeout(10000);
        try {
            getCarH5InfoService getCarH5InfoService = (getCarH5InfoService) factory.create(getCarH5InfoService.class, url);
            Map<String, String> carH5OrderInfo = getCarH5InfoService.getCarH5OrderInfo(mapInfo);
            log.info(carH5OrderInfo.toString());
            map.put("retCode", "0000");
            map.put("retMsg", "执行成功");
            map.put("success", true);
            map.put("data", new Date());
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            map.put("retCode", "0003");
            map.put("retMsg", "调用hessian接口失败");
            map.put("success", false);
            map.put("data", date);
            return map;
        }
    }
}
